#include "GUI.H"
#include "lcd.h"
#include "lcd_init.h"
gui_dev guidev;

xdata timer_db[18] = {1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000, 200000, 500000};
void gui_init()
{
	guidev.posx = 30;
	guidev.posy = 12;
	guidev.high = 54;
	guidev.length = adc_length;
	guidev.grid = TRUE;
	guidev.trig = 1;
	guidev.div = 0;
#if EN_USER_TRIGGER
	guidev.trig_level = 5;
	guidev.trig_levelf = guidev.trig_level / 10.0;
	guidev.trig32 = (u8)(guidev.trig_levelf * 256);
#endif
	guidev.timer = 2; //2us
	guidev.mw = 0;
	guidev.max_vote = guidev.freq = guidev.sum = 0;
	guidev.min_vote = ADC_COUNT;

	guidev.trig_mode_color = trig_way_col;
	guidev.trig_level_color = trig_col;
	guidev.time_level_color = time_col;
	guidev.div_color = div_col;
}
void gui_ref()
{
	guidev.mw = 0;
	guidev.max_vote = guidev.freq = guidev.sum = 0;
	guidev.min_vote = ADC_COUNT;
}

void Gui_linemap(u8 *db, u16 x, u16 y, u16 h, u16 l, u16 color)
{
	u8 i;
	for (i = 0; i < l - 1; i++)
	{
		LCD_DrawLine(i + x, *(db + i) + y, i + x, *(db + i + 1) + y, color);
	}
}

void herz_print()
{
	if (guidev.freq > 999)
	{

		LCD_ShowFloatNum1(84, 68, guidev.freq / 1000.0, 4, BLUE, BLACK);
		LCD_ShowChar(108, 68, 'K', BLUE, BLACK, 0);
	}
	else
	{
		LCD_ShowIntNum(90, 68, guidev.freq, 4, BLUE, BLACK);
	}
	LCD_ShowString(114, 68, "Hz", BLUE, BLACK, 0);
}
void div_print()
{
	LCD_ShowIntNum(126, 68, timer_db[(guidev.timer) % 9], 3, guidev.time_level_color, BLACK);
	if (guidev.timer > 8)
	{
		LCD_ShowString(144, 68, "ms", guidev.time_level_color, BLACK, 0);
	}
	else
	{
		LCD_ShowString(144, 68, "us", guidev.time_level_color, BLACK, 0);
	}
}
void draw_gui(u8 *db, u8 *db_old)
{
	u8 x, y, h, l, i;
	x = guidev.posx;
	y = guidev.posy;
	h = guidev.high;
	l = guidev.length;

	LCD_DrawLine(x, y, x + l, y, LIGHTBLUE);
	LCD_DrawLine(x + l, y, x + l, y + h, LIGHTBLUE);
	LCD_DrawLine(x, y + h, x + l, y + h, LIGHTBLUE);
	LCD_DrawLine(x, y, x, y + h, LIGHTBLUE);
	guidev.freq /= timer_db[guidev.timer];
	Gui_linemap(db_old, x + 1, y, h, l - 1, BLACK);
	Gui_linemap(db, x + 1, y, h, l - 1, wave_col);
	LCD_ShowString(0, 0, " .  - .  V", RED, BLACK, 0);
	LCD_ShowFloatNum1(0, 0, (float)(guidev.min_vote * 3.3 / 256), 3, RED, BLACK);
	LCD_ShowFloatNum1(30, 0, (float)(guidev.max_vote * 3.3 / 256), 3, RED, BLACK);
	LCD_ShowFloatNum1(0, 12, (float)(guidev.sum / adc_length * 3.3 / 256), 3, RED, BLACK);
	LCD_ShowChar(24, 12, 'V', RED, BLACK, 0);
	LCD_ShowChar(66, 0, '~' + 1 + guidev.trig * 2, guidev.trig_mode_color, BLACK, 0);
	LCD_ShowChar(72, 0, '~' + 2 + guidev.trig * 2, guidev.trig_mode_color, BLACK, 0);
	herz_print();
	div_print();
	if (guidev.grid)
	{
		for (i = 0; i < l / 10; i++)
		{
			LCD_DrawPoint(x + l / 2 + i * 5, y + h / 2, grid_color);
			LCD_DrawPoint(x + l / 2 - i * 5, y + h / 2, grid_color);
		}
		for (i = 0; i < h / 10; i++)
		{
			LCD_DrawPoint(x + l / 2, y + h / 2 + i * 5, grid_color);
			LCD_DrawPoint(x + l / 2, y + h / 2 - i * 5, grid_color);
		}
	}
#if EN_USER_TRIGGER
	if (!guidev.div)
		LCD_DrawLine(x, y + h * (1 - guidev.trig_levelf), x + l, y + h * (1 - guidev.trig_levelf), BROWN);
	LCD_ShowFloatNum1(0, 24, (guidev.trig_levelf * 3.3), 3, guidev.trig_level_color, BLACK);
	LCD_ShowChar(24, 24, 'V', guidev.trig_level_color, BLACK, 0);
#endif
	//			delay_ms(40);
}